package com.sbox.model;

import com.sbox.util.SBoxLog;

import android.content.Context;
import android.content.SharedPreferences;

public class SBoxConfig {
	public boolean mFirstTime = true;
	public boolean mNeedPassword = false;
	public String mPasswordHash = "";
	public int mDisplayWidth = 0;
	public int mDisplayHeight = 0;
	public long mLastBackup = 0;
	
	private SBoxConfig()
	{		
	}
	
	private static SBoxConfig sConfig = null;
	private static Context sContext = null;
	public static SBoxConfig getInstance(Context c)
	{
		if( sConfig == null )
		{
			sContext = c;
			sConfig = new SBoxConfig();
			sConfig.loadConfig();
			
		}		
		
		return sConfig;
	}
	
	public void saveConfig()
	{
		SharedPreferences preference = sContext.getSharedPreferences("sbox", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		editor.putBoolean(SBoxConst.KEY_FIRSTTIME, mFirstTime);
		editor.putBoolean(SBoxConst.KEY_NEEDPASSWORD, mNeedPassword);
		editor.putString(SBoxConst.KEY_PASSWORDHASH, mPasswordHash);
		editor.putLong(SBoxConst.KEY_LASTBACKUP, mLastBackup);
		editor.commit();
	}
	
	public void loadConfig()
	{
		SharedPreferences preference = sContext.getSharedPreferences("sbox", Context.MODE_PRIVATE);		
		
		if( !preference.contains(SBoxConst.KEY_FIRSTTIME) )
		{
			mFirstTime = true;
			mNeedPassword = false;
			mLastBackup = 0;
			
			SharedPreferences.Editor editor = preference.edit();
			editor.putBoolean(SBoxConst.KEY_FIRSTTIME, mFirstTime);
			editor.putBoolean(SBoxConst.KEY_NEEDPASSWORD, mNeedPassword);
			editor.putLong(SBoxConst.KEY_LASTBACKUP, mLastBackup);
			editor.commit();
		}
		else
		{	
			SBoxLog.info("loadConfig.");
			mFirstTime = preference.getBoolean(SBoxConst.KEY_FIRSTTIME, true);
			mNeedPassword = preference.getBoolean(SBoxConst.KEY_NEEDPASSWORD, false);
			mPasswordHash = preference.getString(SBoxConst.KEY_PASSWORDHASH, "");
			mLastBackup = preference.getLong(SBoxConst.KEY_LASTBACKUP, 0);
		}				
	}
}
